Skip to content

Add reactive @Secured and JSR-250 method security - #20175

Open
r0h1tb wants to merge 1 commit into
spring-projects:mainfrom
r0h1tb:gh-5103
Open

r0h1tb wants to merge 1 commit into
spring-projects:mainfrom
r0h1tb:gh-5103

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Nov 26, 2026 •

Copy link
Copy Markdown

Closes gh-5103

Reactive method security only supported @PreAuthorize, @PostAuthorize, @PreFilter and @PostFilter. @RolesAllowed, @PermitAll, @DenyAll and @Secured were ignored on Mono/Flux methods, and adding @EnableMethodSecurity(jsr250Enabled = true) to a WebFlux app does not help (see the discussion in the issue).

This adds securedEnabled and jsr250Enabled to @EnableReactiveMethodSecurity, mirroring @EnableMethodSecurity:

@EnableReactiveMethodSecurity(securedEnabled = true, jsr250Enabled = true)
  • core: Jsr250ReactiveAuthorizationManager and SecuredReactiveAuthorizationManager. They delegate to Jsr250AuthorizationManager and SecuredAuthorizationManager, so annotation lookup (class level, interfaces, meta-annotations, the duplicate-annotation check) and the role prefix behave exactly as in the servlet stack. @PermitAll is granted even when no Authentication is present, as it is there. AuthorizationManagerBeforeReactiveMethodInterceptor gets secured() and jsr250() factories with the existing SECURED and JSR250 orders.
  • config: ReactiveSecuredMethodSecurityConfiguration and ReactiveJsr250MethodSecurityConfiguration, imported by ReactiveMethodSecuritySelector when the attributes are set. They pick up GrantedAuthorityDefaults (JSR-250) and RoleHierarchy beans and go through the existing ObjectPostProcessor, so authorization observations work as for @PreAuthorize. The interceptor beans use the securedAuthorizationMethodInterceptor and jsr250AuthorizationMethodInterceptor names that MethodSecurityAdvisorRegistrar already registers as advisors.
  • With useAuthorizationManager = false the attributes fail fast with an IllegalStateException, since the legacy configuration cannot honor them.
  • Docs: a section in the reactive method security page.

Tests: unit tests for both managers and the new interceptor factories, ReactiveSecuredAndJsr250MethodSecurityConfigurationTests covering Mono and Flux methods, class-level and interface annotations (JDK proxy), @PreAuthorize and @RolesAllowed on the same method, both attributes together, the attributes being off by default, GrantedAuthorityDefaults, RoleHierarchy, observations and the useAuthorizationManager = false failure, and KotlinReactiveSecuredAndJsr250MethodSecurityTests for suspend functions and Flow. ./gradlew format and :spring-security-core:check :spring-security-config:check pass locally on JDK 25.

A few choices you may want to weigh in on:

  • The two managers are public, like PreAuthorizeReactiveAuthorizationManager, so they can be used with the new interceptor factories directly. They could be package-private instead if you prefer to keep the API smaller.
  • The interceptor bean names match the servlet ones, as the reactive @PreAuthorize beans already do. So, as today, @EnableMethodSecurity and @EnableReactiveMethodSecurity with the same annotation type enabled cannot share one context.
  • @RolesAllowed and @Secured use a RoleHierarchy bean, as in the servlet stack. Reactive @PreAuthorize expressions do not use one yet, so the two can disagree in the same app. That looks worth a separate issue.
  • I used @since 7.3 since main is at 7.2.1-SNAPSHOT, and the 7.3 whats-new page does not exist yet. Happy to adjust the version or add the entry.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 26, 2026
@EnableReactiveMethodSecurity now has securedEnabled and jsr250Enabled
attributes, as @EnableMethodSecurity does. They add before-method
interceptors backed by the new SecuredReactiveAuthorizationManager and
Jsr250ReactiveAuthorizationManager, which resolve the annotations the
same way as their servlet counterparts and use GrantedAuthorityDefaults
and RoleHierarchy beans when present.

Closes spring-projectsgh-5103

Signed-off-by: Rohit Behera <126186063+r0h1tb@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSR250 support for web-flux

2 participants